<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1201407
-->
<head>
  <title>Test simple manage notification events on MozInputMethodManager</title>
  <script type="application/javascript;version=1.7" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript;version=1.7" src="inputmethod_common.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1201407">Mozilla Bug 1201407</a>
<p id="display"></p>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.7">

let appFrameMM;
let nextStep;

function setupTestRunner() {
  info('setupTestRunner');
  let im = navigator.mozInputMethod;

  let i = 0;
  im.mgmt.onshowallrequest =
  im.mgmt.onnextrequest = nextStep = function(evt) {
    i++;
    switch (i) {
      case 1:
        is(evt.type, 'inputcontextchange', '1) inputcontextchange event');
        appFrameMM.sendAsyncMessage('test:callShowAll');

        break;

      case 2:
        is(evt.type, 'showallrequest', '2) showallrequest event');
        ok(evt.target, im.mgmt, '2) evt.target');
        evt.preventDefault();

        appFrameMM.sendAsyncMessage('test:callNext');

        break;

      case 3:
        is(evt.type, 'nextrequest', '3) nextrequest event');
        ok(evt.target, im.mgmt, '3) evt.target');
        evt.preventDefault();

        im.mgmt.onshowallrequest =
        im.mgmt.onnextrequest = nextStep = null;

        inputmethod_cleanup();
        break;

      default:
        ok(false, 'Receving extra events');
        inputmethod_cleanup();

        break;
    }
  };
}

function setupInputAppFrame() {
  info('setupInputAppFrame');
  return new Promise((resolve, reject) => {
    let appFrameScript = function appFrameScript() {
      let im = content.navigator.mozInputMethod;

      addMessageListener('test:callShowAll', function() {
        im.mgmt.showAll();
      });

      addMessageListener('test:callNext', function() {
        im.mgmt.next();
      });

      im.mgmt.onshowallrequest =
      im.mgmt.onnextrequest = function(evt) {
        sendAsyncMessage('test:appEvent', { type: evt.type });
      };

      im.oninputcontextchange = function(evt) {
        sendAsyncMessage('test:inputcontextchange', {});
      };

      content.document.body.textContent = 'I am a input app';
    };

    let path = location.pathname;
    let basePath = location.protocol + '//' + location.host +
                 path.substring(0, path.lastIndexOf('/'));
    let imeUrl = basePath + '/file_blank.html';

    let inputAppFrame = document.createElement('iframe');
    inputAppFrame.setAttribute('mozbrowser', true);
    inputAppFrame.src = imeUrl;
    document.body.appendChild(inputAppFrame);

    let mm = appFrameMM =
      SpecialPowers.getBrowserFrameMessageManager(inputAppFrame);

    inputAppFrame.addEventListener('mozbrowserloadend', function() {
      mm.addMessageListener('test:appEvent', function(msg) {
        ok(false, 'Input app should not receive ' + msg.data.type + ' event.');
      });
      mm.addMessageListener('test:inputcontextchange', function(msg) {
        nextStep && nextStep({ type: 'inputcontextchange' });
      });
      mm.loadFrameScript('data:,(' + encodeURIComponent(appFrameScript.toString()) + ')();', false);

      // Set the input app frame to be active
      let req = inputAppFrame.setInputMethodActive(true);
      resolve(req);
    });
  });
}

function setupContentFrame() {
  let contentFrameScript = function contentFrameScript() {
    let input = content.document.body.firstElementChild;

    input.focus();
  };

  let iframe = document.createElement('iframe');
  iframe.src = 'file_test_simple_manage_events.html';
  iframe.setAttribute('mozbrowser', true);
  document.body.appendChild(iframe);

  let mm = SpecialPowers.getBrowserFrameMessageManager(iframe);

  iframe.addEventListener('mozbrowserloadend', function() {
    mm.loadFrameScript('data:,(' + encodeURIComponent(contentFrameScript.toString()) + ')();', false);
  });
}

inputmethod_setup(function() {
  Promise.resolve()
    .then(() => setupTestRunner())
    .then(() => setupContentFrame())
    .then(() => setupInputAppFrame())
    .catch((e) => {
      ok(false, 'Error' + e.toString());
      console.error(e);
    });
});

</script>
</pre>
</body>
</html>

